package com.applabs.dnd.core;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * FileName: StoreData.java
 * Description:	Exposes the interfaces for storing data in persistent memory
 * 				Now the implementation supports shared preference for storing
 * @author app-lab
 *
 */
public class StoreData {
	private static SharedPreferences pref = null;
	private Context mContext;
	private static StoreData mStoreData = null;
	
	private StoreData(Context context)
	{
		mContext = context;
	}
	
	/*
	 * Returns a single instance always
	 */
	public static StoreData getInstance(Context context)
	{
		if (mStoreData == null)
		{
			if (context != null)
			{
				mStoreData = new StoreData(context);
			}
		}
		return mStoreData;
	}
	
	/*
	 * data should be serialised by the calling function
	 */
	public void storeData(String key, String data)
	{
		if (pref == null && mContext != null)
		{
			pref = mContext.getSharedPreferences
			(Constant.DND_DATA, Context.MODE_PRIVATE );
		}
		Editor e = pref.edit();
		if (!key.equals(""))
		{
			e.putString(key, data);
		}
		e.commit();
	}
	
	/*
	 * decoding of the string is done by the calling function
	 */
	public String loadData(String key)
	{
		if (pref == null && mContext != null)
		{
			pref = mContext.getSharedPreferences
			(Constant.DND_DATA, Context.MODE_PRIVATE );
		}
		return pref.getString(key, "");
	}
	
	public void removeData(String key)
	{
		if (pref == null && mContext != null)
		{
			pref = mContext.getSharedPreferences
			(Constant.DND_DATA, Context.MODE_PRIVATE );
		}
		Editor e = pref.edit();
		if (!key.equals(""))
		{
			e.remove(key);
		}
		e.commit();
	}
	
}